var map;
var uppolys = [];
var downpolys = [];
var colors = ["#66FF00", "#FF9900", "#0066cc", "#cc33cc", "#FF3399", "#00FFCC"]
var admin = true;

function initialize() {
    var haightAshbury = new google.maps.LatLng(31.86, 117.28);
    var mapOptions = {
	zoom: 13,
	center: haightAshbury,
	mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map =  new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

    for (var i=0; i<6; i++)
    {
	var markersArray = new google.maps.MVCArray();
	var polyOption = {
	    path: markersArray,
	    strokeColor: colors[i],
	    strokeOpacity: 0.8,
	    strokeWeight: 3
	};
	var poly = new google.maps.Polyline(polyOption);
	//	poly.setMap(map);
	uppolys[i] = poly;
	//	poly.setMap(map);
    }
    
    for (var i = 0; i < 6; i++)
    {
	var markersArray = new google.maps.MVCArray();
	var polyOption = {
	    path: markersArray,
	    strokeColor: colors[i],
	    strokeOpacity: 1.0,
	    strokeWeight: 3
	};
	var poly = new google.maps.Polyline(polyOption);
//	google.maps.event.addListener(poly, 'click', function(event) {
//	    addStation(i, event.latlng);
//	});
	downpolys[i] = poly;
	//	poly.setMap(map);
    }
    
    google.maps.event.addListener(map, 'click', function(event) {
	addStation(2,event.latLng);
    });
    reloadLines();
}

function up_state_change(index) {
    downpolys[index].setMap(null);
    if (uppolys[index].getPath().length == 0) {
	$.getJSON('data/up' + (index+1) + '.json', function(data){
	    var poses = data;
	    var poly = uppolys[index];
	    var path = poly.getPath();
	    for (var j = 0; j < poses.length; j++) {
		//				var path = uppolys[i].getPath();
		path.insertAt(path.length,
			      new google.maps.LatLng(poses[j].lat, poses[j].lng));
		if (admin == true) {
		    addMarker(new google.maps.LatLng(poses[j].lat, poses[j].lng));
		}
 	    }
	    poly.setMap(map);
	});
    } else {
	uppolys[index].setMap(map);
    }
}

function down_state_change(index) {
    uppolys[index].setMap(null);
    if (downpolys[index].getPath().length == 0) {
	$.getJSON('data/down' + (index+1) + '.json', function(data){
	    var poses = data;
	    var poly = downpolys[index];
	    var path = poly.getPath();
	    for (var j = 0; j < poses.length; j++) {
		path.insertAt(path.length,
			      new google.maps.LatLng(poses[j].lat, poses[j].lng));
		if (admin == true) {
		    addMarker(new google.maps.LatLng(poses[j].lat, poses[j].lng));
		}
 	    }
	    poly.setMap(map);
	});
    } else {
	downpolys[index].setMap(map);
    }
}

function addStation(index, location) {
    
    var marker = new google.maps.Marker({
	position: location,
	map: map,
	title: "click to edit station"
    });
    var infowindow = new google.maps.InfoWindow();

    google.maps.event.addListener(marker, 'click', function() {
	infowindow.open(map, marker);
    });

    var contectDIV = '<div id="inforMgr">' +
	'<form>' +
	'<br>Station Name: ' + '<input name="nameText" type=text/>' +
	'<br>Arrival  Time: ' + '<input name="timeText" type=text/>' +
	'<br><input onclick="displayOverlays();" type=button value="Save" align="center"/>' +
	'</form>' +
	'</div>';
    infowindow.setContent(contectDIV);
}

function addMarker(location) {
    var marker = new google.maps.Marker({
	position: location, 
	map: map, 
	title:"{\"lat\":" + location.lat() + ",\"lng\":" + location.lng() + "}"
    });
    //    var path = poly.getPath();
    //    path.insertAt(markersArray.length, location);
    //    document.getElementById("path_info").appendChild(document.createTextNode(marker.getTitle()));
    //    document.getElementById("path_info").appendChild(document.createElement("br"));
}

function displayOverlays() {
    var gps;
    for (var i = 0; i < markersArray.length; i++) {
	gps = "(" + markersArray.getAt(i).position.lat() + "," + markersArray.getAt(i).position.lat()+ ")" + gps;
    }
    alert(gps);
    document.getElementById("gps").appendChild(document.createTextNode(gps));
}

function showLine() {
    // clear other displaying line
    //    var path = poly.getPath();
    //    while (path.getLength() != 0) {
    // path.pop();
}

function test() {
    var filename = "";
    var directions = document.getElementsByName("direction");
    for (var i = 0; i < directions.length; i++) {
	if (directions[i].checked == true) {
	    //	    alert(directions[i].value);
	    filename += directions[i].value;
	}
    }
    var lineValues = document.getElementsByName("line");
    for (var i = 0; i < lineValues.length; i++) {
	if (lineValues[i].checked == true) {
	    //	    alert(lineValues[i].value);
	    filename += lineValues[i].value;
	}
    }
    alert(filename + ".json");
    
}

function reloadLines() {
    var direction = "";
    var directions = document.getElementsByName("direction");
    for (var k=0; k < directions.length; k++) {
	if (directions[k].checked == true) {
	    direction += directions[k].value;
	}
    }
    var lineValues = document.getElementsByName("line");
    for (var i = 0; i < lineValues.length; i++) {
	if (lineValues[i].checked == true) {
	    //	    alert(directions[i].value);
	    if (direction == "up") {
		//		if (uppolys[i].getPath().length == 0) { // read from server
		up_state_change(i);
	    } 
	    if (direction == "down") {
		//		if (uppolys[i].getPath().length == 0) { // read from server
		down_state_change(i);
	    }
	} else {
	    if (direction == "down") {
		downpolys[i].setMap(null);
	    }
	    if (direction == "up") {
		uppolys[i].setMap(null);
	    }
	}
    }
}